﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CloudItemContainer.xaml.cs" company="">
//   
// </copyright>
// <summary>
//   Defines the CloudItemContainer type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace devMobile.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media;

    /// <summary>
    /// </summary>
    public partial class CloudItemContainer : UserControl
    {
        /// <summary>
        /// </summary>
        private TextBlock _previousSelection;

        private ObservableCollection<string> _itemsSource;

        /// <summary>
        /// Initializes a new instance of the <see cref="CloudItemContainer"/> class.
        /// </summary>
        public CloudItemContainer()
        {
            InitializeComponent();
            MaxFontSize = 72;
            MinFontSize = 26;
        }

        private void _itemsSource_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            Populate();
        }

        private void Populate()
        {
            var res = from c in _itemsSource
                      orderby c
                      group c by c
                          into c
                          select new Group<string>(c.Key, c.Count());

            // get the max num ocurrencies for ratio
            var max = default(int);
            var acumulativeWidht = default(double);
            var stackRow = new StackPanel
            {
                Orientation = Orientation.Horizontal
            };

            res.ToList().ForEach(
                    r =>
                    {
                        if (r.Items > max)
                        {
                            max = r.Items;
                        }
                    });

            this.LayoutRoot.Children.Clear();

            res.ToList().ForEach(
                c =>
                {
                    var size = (c.Items * MaxFontSize) / max;
                    size = size < MinFontSize ? MinFontSize : size;
                    size = size > MaxFontSize ? MaxFontSize : size;

                    var generator = new Random();

                    var listBoxItem = new TextBlock
                    {
                        FontSize = size,
                        Text = c.Title,
                        Margin =
                            new Thickness(
                            generator.NextDouble() * 10,
                            generator.NextDouble() * 10,
                            generator.NextDouble() * 10,
                            generator.NextDouble() * 10)
                    };

                    listBoxItem.Tap += this.listBoxItem_Tap;

                    if (acumulativeWidht + listBoxItem.ActualWidth > this.ActualWidth)
                    {
                        this.LayoutRoot.Children.Add(stackRow);
                        stackRow = new StackPanel { Orientation = Orientation.Horizontal };
                        acumulativeWidht = default(double);
                    }

                    acumulativeWidht += listBoxItem.ActualWidth;
                    stackRow.Children.Add(listBoxItem);
                });
            this.LayoutRoot.Children.Add(stackRow);
        }

        /// <summary>
        /// </summary>
        public event TagSelectedHandler TagSelected;

        /// <summary>
        /// Gets or sets MaxFontSize.
        /// </summary>
        public double MaxFontSize { get; set; }

        /// <summary>
        /// Gets or sets MinFontSize.
        /// </summary>
        public double MinFontSize { get; set; }

        /// <summary>
        /// Sets ItemsSource.
        /// </summary>
        public IEnumerable<string> ItemsSource
        {
            get
            {
                return _itemsSource;
            }
            set
            {
                _itemsSource = (ObservableCollection<string>)value;
                if (_itemsSource != null)
                    _itemsSource.CollectionChanged += this._itemsSource_CollectionChanged;
                Populate();
            }
        }

        /// <summary>
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The e.
        /// </param>
        private void listBoxItem_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            ((TextBlock)sender).Foreground = new SolidColorBrush(Colors.Blue);
            if (this._previousSelection != null)
            {
                this._previousSelection.Foreground = new SolidColorBrush(Colors.White);
            }

            this._previousSelection = sender as TextBlock;

            if (this.TagSelected != null)
            {
                this.TagSelected(sender, ((TextBlock)sender).Text);
            }
        }
    }
}
